package com.own.business.system.dictionary.base.frame.business.method;

import com.own.business.system.dictionary.adapter.base.BaseDictionaryModel;
import com.own.business.system.dictionary.adapter.method.SystemDictionaryInfoMethod;
import com.own.business.system.dictionary.base.frame.business.model.DictionaryModel;
import com.own.business.system.dictionary.base.frame.business.service.SystemDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * SystemDictionaryInfoMethodImpl
 *
 * @author chenxueli
 * @date 2023-06-21 10:33:00
 */
@Slf4j
@Component
public class SystemDictionaryInfoMethodImpl implements SystemDictionaryInfoMethod {

    @Resource
    private SystemDictionaryService systemDictionaryService;

    /**
     * 根据id获取字典信息
     *
     * @param id 主键id
     * @return 字典信息
     */
    @Override
    public BaseDictionaryModel getItemById(Long id) {
        var item = systemDictionaryService.getPoById(id, false);
        if (item == null) {
            return null;
        }
        return new DictionaryModel(item);
    }

    /**
     * 根据name和rootCode获取id
     *
     * @param name     名称
     * @param rootCode 根code
     * @return id
     */
    @Override
    public Long getIdByNameAndRootCode(String name, String rootCode) {
        return systemDictionaryService.getIdByNameAndRootCode(name, rootCode);
    }

    /**
     * 根据id获取字典信息
     *
     * @param id 主键id
     * @return 字典信息
     */
    @Override
    public BaseDictionaryModel getParentById(Long id) {
        return getItemById(id);
    }

    /**
     * 根据code获取字典信息
     *
     * @param code 数据字典编号
     * @return 字典信息
     */
    @Override
    public BaseDictionaryModel getItemByCode(String code) {
        var item = systemDictionaryService.getByCode(code);
        if (item == null) {
            return null;
        }
        return new DictionaryModel(item);
    }

    /**
     * 根据根code获取字典信息
     *
     * @param code 根code
     * @return 字典信息
     */
    @Override
    public List<DictionaryModel> listByRootCode(String code) {
        var list = systemDictionaryService.mapByDictionaryCode(code);
        return list.stream().map(DictionaryModel::new).toList();
    }

}
